<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends FrontEndController {
    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $aData['pageTitle'] = 'Liên hệ';
        $theme = $this->config->item('front_theme');
        if($theme=='eshopper'){ $aData['no-sidebar'] = true; }
        $this->renderView('static-pages/contact', $aData);
    }

    public function send() {
        $aResult = array();
        $this->lang->load('error', 'vietnamese');
        
        //check captcha
        $gPrivateKey = getOption('google_recaptcha_private_key');
        $this->load->library('recaptcha_lib', 
            array(
                'secret'             => $gPrivateKey, 
                'remote_add'         => $_SERVER["REMOTE_ADDR"], 
                'recaptcha_response' => $_POST["g-recaptcha-response"])
        );
        $resp = $this->recaptcha_lib->verify();
        if (!$resp) {
            $aResult = array(
                'code' => 0,
                'text' => '<ul><li>Xác nhận bằng hình ảnh không đúng</li></ul>',
                'title' => $this->lang->line('contact_fail')
            );
            $this->renderJSON($aResult);
            return;
        }
        
        $contact = $this->input->post('contact', true);
        $this->lang->load('form_validation', 'vietnamese');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('contact[name]',    'lang:contact_name',    'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('contact[email]',   'lang:contact_email',   'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('contact[subject]', 'lang:contact_subject', 'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('contact[message]', 'lang:contact_message', 'trim|required|max_length[10000]|xss_clean');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('contact_fail'),
                'text' => '<ul>' . validation_errors() . '</ul>',
                );
        } else {
            $this->load->model('repo/contact_model', 'contact_model');
            $this->load->model('repo/shop_contact_model', 'contact_model');
            $result = $this->contact_model->save($contact);
            if ($result) {
                $aResult = array('code' => 1, 'title' => $this->lang->line('contact_success'));
            } else {
                $aResult = array('code' => 0, 'title' => $this->lang->line('contact_fail'));
            }
        }
        $this->renderJSON($aResult);
    }
}
?>